import { Tag, Document, DocumentTag } from '../models'

/**
 * 创建文档标签
 * @param  {Int}  userId 用户ID
 * @param  {Int} documentId   文档ID
 * @param  {array} tagId 标签ID
 * @return {Tag} 标签实例
 */
export const createDocumentTag = async (documentId, tagId, userId) => {
  let docTags = await DocumentTag.findOrCreate({
    where: { documentId, tagId },
    defaults: { documentId, tagId, userId }
  })

  return docTags
}

/**
 * 删除文档标签
 * @param  {Int}  userId 用户ID
 * @param  {Int} documentId   文档ID
 * @param  {array} tagId 标签ID
 * @return {Int} 删除记录数
 */
export const removeDocumentTag = async (documentId, tagId, userId) => {
  let rowDeleted = await DocumentTag.destroy({
    where: { documentId, tagId, userId }
  })

  return rowDeleted
}
